<template>
  <div
    class="login-box"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      :size="formSize"
    >
      <div>
        <header>
          <img
            class="login-logo"
            src="@/assets/login_logo.png"
          >
        </header>
      </div>
      <el-form-item
        prop="account"
      >
        <el-input v-model="ruleForm.account">
          <template #prefix>
            <el-icon><user /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item
        prop="pwd"
      >
        <el-input v-model="ruleForm.pwd">
          <template #prefix>
            <el-icon><lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item
        prop="imgcode"
      >
        <div class="ds-box">
          <el-input v-model="ruleForm.imgcode">
            <template #prefix>
              <el-icon><key /></el-icon>
            </template>
          </el-input>
          <!-- <img :src="captchaSrc"> -->
          <img src="@/assets/test.jpg">
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          class="btn"
          :loading="loading"
          @click="submitForm(ruleFormRef)"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { getLoginInfo, login, getCaptcha } from '@/api/common'
import { onMounted, ref, reactive } from 'vue'
import type { ILoginInfo } from '@/api/types/common'
import type { IElForm, IFormRule } from '@/types/element-plus'
import { User, Key, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
// 加载完成后
onMounted(() => {
  console.log(captchaSrc.value)
  console.log('onMounted')
  loadCaptcha()
})
const router = useRouter()
const store = useStore()
// 取接口ILoginInfo里面的slide
const list = ref<ILoginInfo['slide']>([])
console.log(list.value)
getLoginInfo()
// 加载完成后
onMounted(() => {
  loadCaptcha()
})
const captchaSrc = ref('@/assets/test.jpg')
const loadCaptcha = async () => {
  const data = await getCaptcha()
  captchaSrc.value = URL.createObjectURL(data)
}
const loading = ref(false)
const formSize = ref('')
const submitForm = (formEl: IElForm | undefined | null) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      loading.value = true
      console.log('submit!')
      // 登录接口
      const data = await login(ruleForm).catch(() => {
        loadCaptcha()
      }).finally(() => {
        loading.value = false
      })
      // 即使http状态码是成功的但是后端自定义状态码是失败的那么也不会执行下面代码，因为在响应拦截器中手动抛出了一个promise异常
      router.replace({
        name: 'home'
      })
      // 这才是正确代码因为接口请求不了没返回数据导致data.user_info和data.token会报ESlint错误，所以屏蔽
      // store.commit('setUser', {
      //   ...data.user_info,
      //   token: data.token
      // })
      // 伪代码，上面才是正确代码
      store.commit('setUser', {
        ...data,
        token: data
      })
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const ruleFormRef = ref<IElForm | null>(null)
const ruleForm = reactive({
  account: '',
  pwd: '',
  imgcode: ''
})

const rules = reactive<IFormRule>({
  account: [
    {
      required: true,
      message: '请输入账号',
      trigger: 'blur'
    },
    {
      min: 3,
      max: 5,
      message: '长度为3-5个字符',
      trigger: 'blur'
    }
  ],
  pwd: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ],
  imgcode: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'blur'
    }
  ]
})
</script>
<style lang="scss" scoped>
#app {
  height: 100vh;
}
// div {
//   color: $nav-color;
// }
.login-box {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #2b3a4b;
  .login-logo {
    width: 271px;
    height: 74px;
  }
  .btn {
    width: 100%;
  }
  .ds-box {
    display: flex;
  }
}
::v-deep .el-input__prefix {
    align-items: center;
  }
::v-deep .el-form-item__content {
  margin-left: 0 !important;
}
.demo-ruleForm {
  width: 300px;
  padding: 10px 16px;
  background-color: #fff;
  border-radius: 10px;
}
header {
  font-size: 28px;
}

</style>
